package com.suncaper.wcss.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.regexp.internal.RE;
import com.suncaper.wcss.common.PageDTO;
import com.suncaper.wcss.model.domain.AuditHistory;
import com.suncaper.wcss.model.dto.AuditHistoryDTO;
import com.suncaper.wcss.model.vo.ApplicationVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.suncaper.wcss.common.JsonResponse;
import com.suncaper.wcss.service.ApplicationService;
import com.suncaper.wcss.model.domain.Application;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2021-11-16
 * @version v1.0
 */
@Controller
@RequestMapping("/application")
public class ApplicationController {

    private final Logger logger = LoggerFactory.getLogger( ApplicationController.class );

    @Autowired
    private ApplicationService applicationService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Application  application =  applicationService.getById(id);
        return JsonResponse.success(application);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        applicationService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateApplication(@PathVariable("id") Long  id,Application  application) throws Exception {
        application.setId(id);
        applicationService.updateById(application);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建Application
    *
    */
    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Application  application) throws Exception {
        return JsonResponse.success(applicationService.create(application));
    }

    /**
     * 待我审核
     */
    @GetMapping("/wait-audit")
    @ResponseBody
    public JsonResponse waitaudit(PageDTO pageDTO,Application application){
        Page<ApplicationVO> page = applicationService.waitAudit(pageDTO,application);
        return  JsonResponse.success(page);
    }
    /**
     * 单个审核
     */
    @PostMapping("/audit")
    @ResponseBody
    public JsonResponse audit(AuditHistory history){
        applicationService.audit(history);
        return JsonResponse.success(true);
    }

    /**
     * 多个审核
     */
    @PostMapping("/audits")
    @ResponseBody
    public JsonResponse audits(AuditHistoryDTO history){
        applicationService.audits(history);
        return JsonResponse.success(true);
    }

    /**
     * 申请列表
     */
    @GetMapping("/list")
    @ResponseBody
    public JsonResponse list(PageDTO pageDTO,Application application){
        Page<ApplicationVO> page =  applicationService.pageList(pageDTO,application);
        return  JsonResponse.success(page);
    }
    /**
     * 查询某用户在某批次中是否存在申请
     */
    @GetMapping("/getBySnAndBatchId")
    @ResponseBody
    public JsonResponse getBySnAndBatchId(){
        return JsonResponse.success(applicationService.getBySnAndBatchId());
    }

    /**
     * 选择寒衣和规格
     */
    @PostMapping("/check-pro")
    @ResponseBody
    public JsonResponse checkPro(Application application){
        applicationService.checkPro(application);
        return JsonResponse.success(true);
    }
}

